package cn.sumpu.app.guoguojie.web.mvc.misc;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.guoguojie.biz.AlbumDoingBiz;
import cn.sumpu.app.guoguojie.biz.PictorialTopicBiz;
import cn.sumpu.app.guoguojie.domain.DoingPictorial;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;
/**
 * 
 * @author grieous
 * @version $Id: AlbumController.java, v 0.1 2012-9-21 下午03:16:34 grieous Exp $
 */
@Controller
public class AlbumController {
    private final static Logger logger = LoggerFactory.getLogger(AlbumController.class);
    @Autowired
    private AlbumDoingBiz       albumDoingBiz;
    @Autowired
    private PictorialTopicBiz   topicBiz;

    // 进入专辑管理页
    @RequestMapping(value = "/misc/album/album.htm")
    public String album(HttpServletRequest request) {
        return "misc/album/album";
    }

    @RequestMapping(value = "/misc/album/addalbum.htm")
    public String saveAlbum(HttpServletRequest request, String topicId) {
        request.setAttribute("topicId", topicId);
        return "misc/album/saveAlbum";
    }

    @RequestMapping(value = "/misc/album/editalbum.htm")
    public String editAlbum(HttpServletRequest request, String id,String topicId) throws AppBusinessException {
        DoingPictorial pictorial = topicBiz.getPictorialById(id);
        request.setAttribute("pictorial", pictorial);
        request.setAttribute("topicId", topicId);
        return "misc/album/saveAlbum";
    }

    //保存专辑
    @RequestMapping(value = "/misc/pictorial/savepictorial.htm")
    public String saveAlbum(HttpServletResponse response, String topicId, DoingPictorial pictorial, HttpServletRequest request) {
        try {
            pictorial.setMember("superMan");
            pictorial.setPostIp(Common.getIp(request));
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter pw = response.getWriter();
            topicBiz.saveTopicPictorial(topicId, pictorial);
            pw.print("success");
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/album/savealbum.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/album/albumgrid.htm")
    public String albumGrid(Integer page, Integer pagesize, String title, HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        try {
            page = page == null ? 1 : page;
            PageModel pageModel = topicBiz.getPictorial(page, pagesize, title);
            pw = response.getWriter();
            pw.print(JSONObject.fromObject(pageModel));
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/album/albumgrid.htm] fail", e);
            }
        }
        return null;
    }


    // 进入专辑管理页
    @RequestMapping(value = "/misc/album/albumdoing.htm")
    public String albumDetail(HttpServletRequest request, String pictorialId) {
        request.setAttribute("pictorialId", pictorialId);
        try {
            DoingPictorial pictorial = topicBiz.getPictorialById(pictorialId); 
            request.setAttribute("pictorial", pictorial);
        } catch (AppBusinessException e) {
            logger.error("[/misc/album/albumdoing.htm] fail", e);
        }
        return "misc/album/albumDoing";
    }

    @RequestMapping(value = "/misc/album/albumdoinggrid.htm")
    public String albumDoingGrid(Integer page, Integer pagesize, String pictorialId, HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        try {
            page = page == null ? 1 : page;
            PageModel pageModel = topicBiz.getPictorialDoing(pictorialId, page, pagesize);
            pw = response.getWriter();
            pw.print(JSONObject.fromObject(pageModel));
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/album/albumgrid.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/album/deletealbumdoing.htm")
    public String deletePictorial(String pictorialId, String doingId, HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter pw = null;
        try {
            topicBiz.deletePictorialDoing(pictorialId, doingId);
            pw = response.getWriter();
            pw.print("success");
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/album/deletealbum.htm] fail", e);
            }
        }
        return null;
    }


    @RequestMapping(value = "/misc/album/pushtoalbum.htm")
    public String pushToAlbum(HttpServletRequest request, HttpServletResponse response, String doingIds, String pictorialIds) {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = null;
        try {
            String res = albumDoingBiz.pustToAlbum(pictorialIds, doingIds);
            pw = response.getWriter();
            pw.print(res);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/album/pushtoalbum.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/topic/movetopic.htm")
    public String moveTopic(String topicId, String moveType, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter pw = null;
        try {
            topicBiz.moveTopicSort(topicId, moveType);
            pw = response.getWriter();
            pw.print("success");
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/topic/movetopic.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/topic/movepictorial.htm")
    public String movePictorial(String topicId, String pictorialId, String moveType, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter pw = null;
        try {
            topicBiz.movePictorialSort(topicId, pictorialId, moveType);
            pw = response.getWriter();
            pw.print("success");
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/topic/movepictorial.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/topic/movedoing.htm")
    public String moveDoing(String pictorialId, String doingId, String moveType, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter pw = null;
        try {
            topicBiz.moveDoingSort(pictorialId, doingId, moveType);
            pw = response.getWriter();
            pw.print("success");
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/topic/movedoing.htm] fail", e);
            }
        }
        return null;
    }

    @RequestMapping(value = "/misc/topic/setcover.htm")
    public String setPictorialCover(String pictorialId, String cover, HttpServletResponse response) {
        response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter pw = null;
        try {
            topicBiz.setPictorialCover(pictorialId, cover);
            pw = response.getWriter();
            pw.print("success");
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/misc/topic/setcover.htm] fail", e);
            }
        }
        return null;
    }
}
